﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Xml.Serialization;


namespace LegacyDocInput
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string filePath;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void confirm_Click(object sender, EventArgs e)
        {
            try
            {
                var doc = new legacydoc();

                doc.bh = txtbh.Text;
                doc.rq = DateTime.Parse(txtrq.Text, CultureInfo.InvariantCulture);
                doc.qy = txtqy.Text;
                doc.zld = txtzld.Text;
                doc.wtr = txtwtr.Text;
                doc.mj = decimal.Parse(txtmj.Text, CultureInfo.InvariantCulture);
                doc.zlc = ushort.Parse(txtzlc.Text, CultureInfo.InvariantCulture);
                doc.shh = txtshh.Text;
                doc.zhx = txtzhx.Text;
                doc.ly = txtly.Text;
                doc.jgrq = DateTime.Parse(txtjgrq.Text, CultureInfo.InvariantCulture);
                doc.pgdj = decimal.Parse(txtpgdj.Text, CultureInfo.InvariantCulture);
                doc.pgzj = decimal.Parse(txtpgzj.Text, CultureInfo.InvariantCulture);
                doc.pgr = txtpgr.Text;
                doc.gjshd = DateTime.Parse(txtgjshd.Text, CultureInfo.InvariantCulture);
                doc.bgwchrq = DateTime.Parse(txtbgwchrq.Text, CultureInfo.InvariantCulture);
                doc.bzh = txtbzh.Text;

                string path = "";

                if (string.IsNullOrEmpty(filePath))
                {
                    path = string.Format(CultureInfo.InvariantCulture,
                        "data\\{0}.xml", DateTime.Now.ToFileTime());
                }
                else
                {
                    path = filePath;
                }

                using(FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(legacydoc));
                    xs.Serialize(fs, doc);
                }

                clear();

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void clear()
        {
            foreach (var ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)ctrl).Text = null;
                }
            }

            filePath = "";
        }

        private void txtpgzj_Enter(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtpgdj.Text) &&
                    !string.IsNullOrEmpty(txtmj.Text))
                {
                    var pgdj = decimal.Parse(txtpgdj.Text, CultureInfo.InvariantCulture);
                    var mj = decimal.Parse(txtmj.Text, CultureInfo.InvariantCulture);
                    txtpgzj.Text = (mj * pgdj).ToString();
                }

                txtpgzj.SelectAll();
            }
            catch
            {
            }
        }

        private void btOpen_Click(object sender, EventArgs e)
        {
            clear();

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();

            var fn = ofd.FileName;


            if (!string.IsNullOrEmpty(fn))
            {

                legacydoc doc;

                using (FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(legacydoc));
                    doc = (legacydoc)xs.Deserialize(fs);
                }

                txtbh.Text = doc.bh;
                txtrq.Text = doc.rq.ToShortDateString();
                txtqy.Text = doc.qy;
                txtzld.Text = doc.zld;
                txtwtr.Text = doc.wtr;
                txtmj.Text = doc.mj.ToString();
                txtzlc.Text = doc.zlc.ToString();
                txtshh.Text = doc.shh;
                txtzhx.Text = doc.zhx;
                txtly.Text = doc.ly;
                txtjgrq.Text = doc.jgrq.ToShortDateString();
                txtpgdj.Text = doc.pgdj.ToString();
                txtpgzj.Text = doc.pgzj.ToString();
                txtpgr.Text = doc.pgr;
                txtgjshd.Text = doc.gjshd.ToShortDateString();
                txtbgwchrq.Text = doc.bgwchrq.ToShortDateString();
                txtbzh.Text = doc.bzh;

                this.filePath = fn;
            }
        }
    }
}
